package com.jixi;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class JWT_TEST {
	public static void main(String[] args) {
		System.out.println(JWT_TEST.getJWTForCoze(1864695777419452418L));
	}
	
	private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	private static final Random random = new Random();
	
	public static String generateRandomString(int length) {
		StringBuilder sb = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			int index = random.nextInt(ALPHA_NUMERIC_STRING.length());
			sb.append(ALPHA_NUMERIC_STRING.charAt(index));
		}
		return sb.toString();
	}
	
	public static String getJWTForCoze(Long educationId) {
		Map<String, Object> Header = new HashMap<String, Object>();
		Header.put("alg", "RS256");
		Header.put("typ", "JWT");
		Header.put("kid", "lPtNOfYPqNnPHhNnEv1tYcgxtypuTWbwAcNOxqow4ig");
		
		long now_time = Instant.now().getEpochSecond();
		Map<String, Object> Payload = new HashMap<String, Object>();
		Payload.put("iss", "1137650355553");
		Payload.put("aud", "api.coze.cn");
		Payload.put("iat", now_time);
		Payload.put("exp", now_time + 60 * 60 * 24);
		Payload.put("jti", generateRandomString(1));
		Payload.put("session_name", String.valueOf(educationId));
		
		String privateKeyContent = "-----BEGIN PRIVATE KEY-----\n" +
				"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDaokhO/cIGbFzL\n" +
				"VudviBYzBkYeKCfTkVqRQYrVC8kVk3r8vPpAEMH3wPsolD1EbHo3LUSLv+8ia18m\n" +
				"y4aetQHGeXlXStzcfKIBhhPuv3zgISn75GZT88mhlTgTNaZbYt8oiI3Ljpt05SNU\n" +
				"ubnEre3tZbkyuYJlqYmbhwGsunAAWNyHB26NhzaTd0XpraTi56Hyd3Zo2TAca/qS\n" +
				"BIN9vTLn66mvK2Uaxrft7h2eAvNI+GHNTGVf7phPeMqr+ZlZbjG9coc7D4bUkhR0\n" +
				"ZEwLMbygBHoOXtHEOQ67FfnK9p5yxgyBcA2zyVeGKgEjDtfrUXYD6gO0Iy3tP2Ly\n" +
				"cVmYnTT1AgMBAAECggEADGuZQ9wYdijPo9y4E4krZImrWRI7EGdRlXfpFjtv8+jn\n" +
				"uF8zHkB1dZsLCZ4K1dcNxhI/e7QTJFwSLGJjr35JplEHT05rgzEVFrl//RllnMUa\n" +
				"HR+3ena/EkTMEzdLYkph+34l33FLLEPKJWT3X8d7uBwqotvZw55OggZvw/2fIWJi\n" +
				"URaFkzvvccMNwHM5SyBdwZvALkLCSTjhodbqeRLGqzftaYKSyAGClka6Zm+b7HKC\n" +
				"3WwSD65566P4MYPOF37+8z7sIzIDfkwqBZ06/IGIwEzv6orCGmeiGrtlQ3L1KL9Z\n" +
				"PVP31ZNwTvZ2oYajZrk9gTeP6ks81d2iwMrlq9iobwKBgQD5lIZp6rrvy/Tp7vUh\n" +
				"YwX+XwyAiqilaet7pH0XN1dRDpyfgTTsCviagAKn9yhgcbj8/jg3KpyARt7CQyP7\n" +
				"b16xiVB4BGhRGTuyXTeCGcv128Cmsi3fXAhGut+orIFaLWKugBpXVjXjwOUjA27c\n" +
				"qmXN6sdtj7ibBAVYNr9HGks0wwKBgQDgQfpC0SqUNu5DW6PoeER5tRhfvlQGG9sg\n" +
				"jb9QQsGWSpW++EHI6qeLNdQxElSkkTq8Y8SToxveiDL7cb+0VEKKGm9rA+rO3UtU\n" +
				"fwFoqQBRRXezRmWaPXCPkpBYH6LoBk9LguFCpMN4PeuDD5cTu2NLPXrvPvtQxWjQ\n" +
				"vlysnO9z5wKBgBNcbkDxMKdBjzJGk0WPxPtCrFmcaBDM8rOYwwt2A5QKKiRFhbF5\n" +
				"+Gcj3xDgn4U7zTnzDrFY4OMinE09oCD1NsdW+6MT1HPWHUds681v6t6buYo08FIP\n" +
				"daTnAkq3dyFju/AFiAWWbn/Iiv34WjVMErGysN3S9GMyIKgDNztw0ry5AoGAP2Zn\n" +
				"ujwoLX7aHXDrBe36be4k1eyZFKELckigrbAxyquLalIFRygP9eth8f2csGnekSQj\n" +
				"jxmt51vaDfw6lmC77w9KMJcK6zDk+x5s54nf7RoWnThpwRFdoTluVXucMryV7mMy\n" +
				"DAVIvdXDn2no7Est5zeaackEg/FLkLhAuwwjJkECgYA6SArHhzt98uYoPnkoeN7w\n" +
				"Dphdur3IXnccScU2d65y6Zp/hry9S2tJ1irA6E2wFxQTuL2wbVjRJzMKg8jcmZVp\n" +
				"4ey26ANyg/i5lohKvqTdOWURJMsWWu4zmrdqXr7MRTTjsci/WBcrtzUgCzuQCdA7\n" +
				"RQ54Q2wSQnZp3rbuxtQetw==\n" +
				"-----END PRIVATE KEY-----";
		
		// 移除私钥中的头尾和换行符
		privateKeyContent = privateKeyContent.replace("-----BEGIN PRIVATE KEY-----", "")
				.replace("-----END PRIVATE KEY-----", "")
				.replaceAll("\\s", "");
		
		try {
			byte[] decoded = Base64.getDecoder().decode(privateKeyContent);
			PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
			KeyFactory kf = KeyFactory.getInstance("RSA");
			PrivateKey privateKey = kf.generatePrivate(spec);
			
			String JWTToken = Jwts.builder()
					.setHeader(Header)
					.setClaims(Payload)
					.signWith(SignatureAlgorithm.RS256, privateKey)
					.compact();
			
			return JWTToken;
			
		} catch (Exception e) {
			throw new RuntimeException("生成JWT失败", e);
		}
	}
	
}
